import 'dart:math';

import 'package:asperm/pages/start_testing_page.dart';
import 'package:asperm/tools/const.dart';
import 'package:asperm/tools/tools.dart';
import 'package:asperm/views/custom_cell.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_picker/image_picker.dart';

import '../tools/configuration.dart';
import '../tools/motility.dart';
import '../views/myDialog.dart';


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final ImagePicker _picker = ImagePicker();


  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xffF9F9F9),
        body: Container(
          height: screenHeight(context),
          child: Container(
            width: double.infinity,
            color: Color(0xffF9F9F9),
            child: contentWidget(context),
          ),
        )
    );
  }

  contentWidget(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: appBarHeight(context)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // SvgPicture.asset(
          //   'assets/device2.svg',  // 本地 SVG 文件路径
          //   width: 300,
          //   height: 300,
          //   fit: BoxFit.fill,
          // ),
         Container(
           child:  Image.asset('images/device2.png',height: 200,width: 200,),
         ),
          SizedBox(height: 40.h,),
          centerMenuWidget(context),
          SizedBox(height: 40.h,),
          Container(
            height: 48.h,
            width: 335.w,
            decoration: BoxDecoration(
              color: Color.fromRGBO(16, 37, 105, 1),
              borderRadius: BorderRadius.circular(5.h),
            ),
            margin: EdgeInsets.only(left: 20.w, right: 20.w, top: 20.h),
            child: TextButton(
              onPressed: () {

                Navigator.pushNamed(context, '/start_testing_page');
              },
              child: Text(
                 'START TESTING',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w400,

                ),
              ),
            ),
          ),
          Container(
            height: 48.h,
            width: 335.w,
            decoration: BoxDecoration(
              color: Color.fromRGBO(16, 37, 105, 1),
              borderRadius: BorderRadius.circular(5.h),
            ),
            margin: EdgeInsets.only(top: 20.h),
            child: TextButton(
              onPressed: _pickVideo,
              child: Text(
                '相册读取视频',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  centerMenuWidget(BuildContext context){
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: 20.w,right: 20.w),
          child: CustomCell(
            title: 'Tips',
            imageName: '',
            clickAction: (){
              Navigator.pushNamed(context, '/tips_page');

            },
          ),
        ),
        Container(
          height: 0.5.h,
          color: Color(0xffEEEEEE),
          margin: EdgeInsets.only(left: 20.w, right: 20.w),
        ),
        Container(
          margin: EdgeInsets.only(left: 20.w,right: 20.w),
          child: CustomCell(
            title: 'History',
            imageName: '',
            clickAction: (){
              Navigator.pushNamed(context, '/history_page');

            },
          ),
        ),
        Container(
          height: 0.5.h,
          color: Color(0xffEEEEEE),
          margin: EdgeInsets.only(left: 20.w, right: 20.w),
        ),
        Container(
          margin: EdgeInsets.only(left: 20.w,right: 20.w),
          child: CustomCell(
            title: 'Buy now',
            imageName: '',
            clickAction: (){
              Navigator.pushNamed(context, '/buy_page');

            },
          ),
        ),
        Container(
          height: 0.5.h,
          color: Color(0xffEEEEEE),
          margin: EdgeInsets.only(left: 20.w, right: 20.w),
        ),
        Container(
          margin: EdgeInsets.only(left: 20.w,right: 20.w),
          child: CustomCell(
            title: 'My',
            imageName: '',
            clickAction: (){
              Navigator.pushNamed(context, '/my_page');

            },
          ),
        ),
      ],
    );
  }

  // 从相册选择视频
  Future<void> _pickVideo() async {
    // 调用相册选择视频，可指定最大时长（单位：秒）
    showLoading('计算中，请稍等...');
    await _picker.pickVideo(source: ImageSource.gallery).then((file) async {
      if (file != null) {
        print('------文件不是空的');

        await basicUsage(file.path);

      }else{
        hideLoading();
        showToast('文件是空的');
        print('------文件是空的');

      }
    });
  }
  /// 示例：基本使用
   Future<void> basicUsage(String videoPath) async {
    print('=== 基本使用示例 ===');

    // 1. 创建配置
    final config = Configuration();

    // 2. 创建Motility实例
    final motility = Motility(config: config);

    // 3. 分析视频
    if (videoPath != null) {
      print('开始分析视频: $videoPath');

      final result = await motility.analyze(videoPath);
      hideLoading();

      if (result != null) {

        Navigator.pushNamed(context, '/report_page',arguments: result);
      } else {
        showToast('分析失败或没有检测到精子');
        print('=====分析完成！ ==== 分析失败或没有检测到精子');
      }
    } else {
      hideLoading();
      showToast('未找到示例视频文件');
      print('=====未找到示例视频文件');
    }
  }

  // showResultDialog(String result){
  //   showDialog(
  //       context: context,
  //       builder: (context) {
  //         return customAlertDialog(
  //             title: '检测结果',
  //             describe: result,
  //             leftStr: '',
  //             rightStr: '关闭',
  //             onTextChanged: (value) async {
  //             },
  //             sureFun: () async {
  //               Navigator.pop(context);
  //             },
  //             cancelFun: () {
  //               Navigator.pop(context);
  //             });
  //       });
  // }




}

